/**
 *    SPDX-License-Identifier: Apache-2.0
 */

const requtil = require('./requestutils');

/**
 *
 *
 * @param {*} router
 * @param {*} platform
 */
const openroutes = async function(router, platform) {
	const dbStatusMetrics = platform.getPersistence().getMetricService();
	const dbCrudService = platform.getPersistence().getCrudService();

	/**
	 * Transactions by Organization(s)
	 * GET /getLastBlockNumber
	 * curl -i 'http://<host>:<port>/open/getLastBlockNumber'
	 * Response:
	 * {"status": 200,"detail": [{"blocknum": 58,"txcount": 1,"blockhash": "ee3e69896bc740d3e816877953343ccfdbf3622bb253507cb489b2fed6325ea6","network_name": "test-network","createdt": "2020-05-21T06:57:34.000Z"}]}
     * 
     *
	 */
	router.get('/getLastBlockNumber', async (req, res) => {
        const row = await dbCrudService.getLastBlockNumber()
        if (row) {
            return res.send({
                status: 200,
                detail: row
            });
        } else {
            return res.send({ status: 401, 'detail': 'is not fount' });
        }
    });
    /**
	 * Transactions by Organization(s)
	 * GET /getTxDetailByBlockNum
	 * curl -i 'http://<host>:<port>/open/getTxDetailByBlockNum/<blockid>'
	 * Response:
	 * { "status": 200,"detail": [{"blockid": 54,"txhash": "238f7c63c4d98d0015a502103c77bab6c17deaefaf81eda7ab1fa954ca151f7f""chaincodename": "assets","creator_msp_id": "Org1MSP","network_name": "test-network","createdt": "2020-05-21T05:46:45.000Z"}]}
     * 
     *
	 */
    router.get('/getTxDetailByBlockNum/:blockid', async (req, res) => {
        const blockid = parseInt(req.params.blockid);
        const row = await dbCrudService.getTxDetailByBlockNum(blockid)
        if (row) {
            return res.send({
                status: 200,
                blockid:blockid,
                detail: row
            });
        } else {
            return res.send({ status: 4004, 'detail': 'blocknum is not fount' });
        }
		
    });
}; // End platformroutes()

module.exports = openroutes;

